<template>
    <a-card :bordered="false">

        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <template>
                        <a-col :md="6" :sm="8">
                            <a-form-item label="名称">
                                <a-input placeholder="请输入名称查询" v-model="queryParam.name"></a-input>
                            </a-form-item>
                        </a-col>
                    </template>

                    <a-col :md="6" :sm="8">
						<span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
							<a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
							<a-button type="primary" @click="resetParam" icon="reload" style="margin-left: 8px">重置
							</a-button>
						</span>
                    </a-col>

                </a-row>
            </a-form>
        </div>

        <!-- 操作按钮区域 -->
        <div class="table-operator" style="border-top: 5px">
            <a-button @click="addDev" type="primary" icon="plus">添加级联</a-button>
            <a-dropdown v-if="selectedRowKeys.length > 0">
                <a-menu slot="overlay" @click="handleMenuClick">
                    <a-menu-item key="1">
                        <a-icon type="delete" @click="batchDel"/>
                        删除
                    </a-menu-item>
                </a-menu>
                <a-button style="margin-left: 8px">
                    批量操作
                    <a-icon type="down"/>
                </a-button>
            </a-dropdown>
        </div>

        <!-- table区域-begin -->
        <div>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
                <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a
                    style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
                <a style="margin-left: 24px" @click="onClearSelected">清空</a>
            </div>

            <a-table ref="table" bordered size="middle" rowKey="devId" :columns="columns" :dataSource="dataSource"
                     :pagination="ipagination" :loading="loading"
                     :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
                     @change="handleTableChange">

            <span slot="action" slot-scope="text, record">
            <a-popconfirm title="确定启用吗?" v-if="record.state == 3" @confirm="() => changeStatus(record,1)">
                <a>启用</a>
            </a-popconfirm>
            <a-popconfirm title="确定停用吗?" v-if="record.state == 1 || record.state == 2" @confirm="() => changeStatus(record,0)">
                <a>停用</a>
            </a-popconfirm>
            <a-divider type="vertical"/>
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.platformId)">
                <a>删除</a>
            </a-popconfirm>
            </span>
            </a-table>
        </div>
        <!-- table区域-end -->
        <platform-add-modal ref="modalForm" @ok="modalFormOk"></platform-add-modal>
        <platform-edit-modal ref="deveditmodal" @ok="editModalOk"></platform-edit-modal>
    </a-card>
</template>

<script>
    import PlatformAddModal from './modules/PlatformAddModal'
    import PlatformEditModal from './modules/PlatformEditModal'
    import {
        JeecgListMixin
    } from '@/mixins/JeecgListMixin'
    import {
        getAction,
        postAction
    } from '@/api/manage'

    export default {
        name: "PlatformList",
        mixins: [JeecgListMixin],
        components: {
            PlatformAddModal,
            PlatformEditModal
        },
        data() {
            return {
                description: '这是级联管理页面',
                queryParam: {
                    name: undefined,
                    modelId: undefined,
                    online: undefined
                },
                modelList: [],
                columns: [{
                    title: '名称',
                    align: "center",
                    dataIndex: 'name',
                    width: 60
                },
                    {
                        title: '标识',
                        align: "center",
                        dataIndex: 'serverGBId',
                        width: 60
                    },
                    {
                        title: 'IP',
                        align: "center",
                        width: 60,
                        dataIndex: 'serverIP'
                    },
                    {
                        title: '端口',
                        align: "center",
                        width: 60,
                        dataIndex: 'serverPort'
                    },
                    {
                        title: '信令传输模式',
                        align: "center",
                        width: 60,
                        dataIndex: 'transport'
                    },
                    {
                        title: '连接类型',
                        align: "center",
                        width: 60,
                        dataIndex: 'connectType',
                        customRender: function (text) {
                            if (text == 1) {
                                return "互联"
                            } else if (text == 2) {
                                return "级联"
                            }
                        }
                    },
                    {
                        title: '角色',
                        align: "center",
                        width: 60,
                        dataIndex: 'role',
                        customRender: function (text) {
                            if (text == 1) {
                                return "下级"
                            } else if (text == 2) {
                                return "上级";
                            } else {
                                return "-";
                            }
                        }
                    },
                    {
                        title: '状态',
                        align: "center",
                        width: 60,
                        dataIndex: 'state',
                        customRender: function (text) {
                            if (text == 1) {
                                return "在线"
                            } else if (text == 2) {
                                return "下线"
                            } else if (text == 3) {
                                return "停用"
                            }
                        }
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        scopedSlots: {
                            customRender: 'action'
                        },
                        align: "center",
                        width: 90
                    }
                ],
                url: {
                    list: "/platform/page",
                    delete: "/platform/delete",
                    deleteBatch: "/platform/delete-batch"
                },
            }
        },
        methods: {
            addDev: function () {
                this.$refs.modalForm.show();
                this.$refs.modalForm.disableSubmit = false;
                this.$refs.modalForm.modelList = this.modelList;
            },
            modalFormOk() {
                // 新增/修改 成功时，重载列表
                this.loadData();
            },
            editDev: function (record) {
                this.$refs.deveditmodal.show(record);
                this.$refs.deveditmodal.disableSubmit = false;
                this.$refs.deveditmodal.modelList = this.modelList;
            },
            editModalOk() {
                // 修改 成功时，重载列表
                this.loadData();
            },
            devDetail: function (record) {
                this.$refs.deveditmodal.show(record);
                this.$refs.deveditmodal.title = "详情";
                this.$refs.deveditmodal.disableSubmit = true;
            },
            handleMenuClick(e) {
                if (e.key == 1) {
                    this.batchDel();
                }
            },
            changeStatus(record, enable) {
                let objTemp = {}
                // objTemp.enable = (record.enable == 0) ? 1 : 0
                objTemp.enable = enable
                objTemp.platformId = record.platformId
                postAction("/platform/set-enable", objTemp).then(res => {
                    if (res.code == '00000') {
                        this.$message.success("修改成功")
                        this.loadData()
                    } else {
                        this.$message.warning("修改失败:" + res.msg)
                    }
                })
            },
            resetParam() {
                this.queryParam = {}
                this.queryParam.userType = undefined
                this.loadData(1)
            },
            //路由跳转，打开新的tab
            cameraList(record) {
                console.log(JSON.stringify(record.devId))
                //关闭tab
                // this.$parent.closeRouteViewTab('dev/camera')

                //跳转新tab
                this.$router.push({
                    name: '/dev/camera',
                    params: {
                        devId: record.devId
                    }
                })
                // this.routerRefresh();//调用app.vue里面的routerRefresh()方法，完成摧毁和重建过程
            }
        }
    }
</script>
<style scoped>
    @import '~@assets/less/common.less'
</style>
